package com.zhuss.car.processor;

import java.util.*;

import com.zhuss.car.processor.cache.CarCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.car.car.UpdateCarBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.car.db.dao.ext.CarDao;
import com.zhuss.car.db.entity.Car;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * @description: 车辆修改
 * @author: zhushanshuo
 * @create: Fri Dec 18 20:35:18 CST 2020
 **/
@Component("carUpdate")
public class CarUpdate extends AbstractProcessor<Object, UpdateCarBo> implements Processor<Object, UpdateCarBo> {

    @Autowired
    private CarDao carDao;

    @Autowired
	@Qualifier("carRead")
	private CarRead carRead;

	@Autowired
	@Qualifier("carCache")
	private CarCache carCache;

    /**
     * 校验
     */
    @Override
    public void check(UpdateCarBo bo) throws DzcxException {
    	Long carId = bo.getCarId();
    	String plateNo = bo.getPlateNo();
		if (bo.getRideType() == null || bo.getRideType()== 0) {
			throw new DzcxException(ExceptionCode.乘车类型为空);
		}

		if (bo.getType() == null || bo.getType()== 0) {
			throw new DzcxException(ExceptionCode.车辆类型为空);
		}

		if (bo.getPeopleQuantity() == null || bo.getType()== 0) {
			throw new DzcxException(ExceptionCode.核载数量为空);
		}

		if (bo.getCompanyId() == null || bo.getCompanyId() <=0l) {
			throw new DzcxException(ExceptionCode.所属公司为空);
		}

    	if (carId == null || carId <= 0l) {
    		throw new DzcxException(ExceptionCode.车辆ID为空);
    	}
    	if (plateNo == null || "".equals(plateNo.trim())) {
    		throw new DzcxException(ExceptionCode.车辆plateNo为空);
    	}

    	List<Car> list = carRead.listNotId(plateNo, carId);
    	if (!list.isEmpty()) {
    		throw new DzcxException(ExceptionCode.车辆plateNo已存在);
    	}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(UpdateCarBo param) {
    	Car record = new Car();
    	record.setCarId(param.getCarId());
    	record.setPlateNo(param.getPlateNo());
    	record.setType(param.getType());
    	record.setCompanyId(param.getCompanyId());
    	record.setDutyUser(param.getDutyUser());
    	record.setDutyPhone(param.getDutyPhone());
    	record.setPeopleQuantity(param.getPeopleQuantity());
    	record.setStatus(param.getStatus());
    	record.setPic(param.getPic());
    	record.setSort(param.getSort());
		record.setRideType(param.getRideType());
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	carDao.updateByPrimaryKeySelective(record);
		carCache.clear(record.getCarId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateCarBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.车辆.key());
    	record.setAction(LogActionEnum.车辆修改.key());
    	record.setTargetId(param.getCarId());
    	record.setTargetName(param.getPlateNo());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
